/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import connection.AbstractConnection;
import entidades.Uf;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Ricardo
 */
public class UfDAO extends AbstractDAO {

    private final String sqlFindUf = "select iduf,estado from uf where iduf=?";
    private final String sqlDeleteUf = "delete from uf where iduf=?";
    private final String sqlInsertUf = "insert into uf (iduf,estado) values (?,?)";

    public UfDAO(AbstractConnection conn) {
        super(conn);
    }

    public Uf encontraUf(String idUf) throws SQLException {
        Uf uf = null;

        PreparedStatement ps = conn.getConnection().prepareStatement(sqlFindUf);

        ps.setString(1, idUf);

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            uf = new Uf();
            uf.setEstado(rs.getString("estado"));
            uf.setUf(rs.getString("iduf"));
        }

        conn.closeConnection();

        return uf;
    }

    public void remover(Uf uf) throws SQLException {
        if (encontraUf(uf.getUf()) != null) {
            PreparedStatement ps = conn.getConnection().prepareCall(sqlDeleteUf);
            ps.setString(1, uf.getUf());
            ps.executeQuery();
        }
    }

    public void insertUpdate(Uf uf) throws SQLException {
        remover(uf);
        PreparedStatement ps = conn.getConnection().prepareStatement(sqlInsertUf);
        ps.setString(1, uf.getUf());
        ps.setString(2, uf.getEstado());
        ps.executeQuery();
    }
}
